package vn.egame.elockscreen.view;

import vn.egame.elockscreen.R;
import vn.egame.elockscreen.tools.TimerManager;
import vn.egame.elockscreen.utils.Fonts;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * TODO: document your custom view class.
 */
public class TimeView extends RelativeLayout {
	private View timeLayout;
	private TextView tvDate, tvTime;
	private Context mContext;

	private final Handler mHandler = new Handler();

	private BroadcastReceiver timeReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
				setTime();
			}
		}
	};

	private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
		public void onReceive(Context paramAnonymousContext,
				Intent paramAnonymousIntent) {
			String str = paramAnonymousIntent.getAction();
			if (str.equals("android.intent.action.SCREEN_ON")) {
				// ScreenLockerBackground.this.invalidate();
				Log.d("Kai", "SCREEN_ON");
				setTime();
				mContext.registerReceiver(timeReceiver, new IntentFilter(
						Intent.ACTION_TIME_TICK));
			} else if (str.equals("android.intent.action.SCREEN_OFF")) {
				Log.d("Kai", "SCREEN_OFF");
				mContext.unregisterReceiver(timeReceiver);
			} else if (str.equals("vn.egame.elockscreen.activity.ON_DESTROY")) {
				Log.d("Kai", "ON_DESTROY");
				mContext.unregisterReceiver(timeReceiver);
			}
		}
	};

	private void setTime() {

		Log.d("Kai", "Set Time " + TimerManager.getTime());

		tvDate.setText(TimerManager.getDate());
		tvTime.setText(TimerManager.getTime());

	}

	public TimeView(Context context) {
		super(context);
		mContext = context;
		Fonts.getInstance().init(mContext);
		timeLayout = View.inflate(context, R.layout.layout_module_launchtime,
				this);
		TimerManager.getInstance().init(mContext);
		initViews();
		setTime();
	}

	private void initViews() {

		tvDate = (TextView) timeLayout.findViewById(R.id.tvDate);
		tvTime = (TextView) timeLayout.findViewById(R.id.tvTime);
		tvDate.setTypeface(Fonts.getInstance().fontLight);
		tvTime.setTypeface(Fonts.getInstance().fontThin);
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		// Toast.makeText(mContext, "u can see me", Toast.LENGTH_SHORT).show();
		mContext.registerReceiver(timeReceiver, new IntentFilter(
				Intent.ACTION_TIME_TICK));

		IntentFilter localIntentFilter = new IntentFilter();
		localIntentFilter.addAction("android.intent.action.SCREEN_ON");
		localIntentFilter.addAction("android.intent.action.SCREEN_OFF");
		localIntentFilter.addAction("vn.egame.elockscreen.activity.ON_DESTROY");

		getContext().registerReceiver(this.mIntentReceiver, localIntentFilter);
		// getContext().registerReceiver(this.mIntentReceiver,
		// localIntentFilter,
		// null, this.mHandler);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		// Toast.makeText(mContext, "u can't see me anymore",
		// Toast.LENGTH_SHORT)
		// .show();
		Log.d("Kai", "onDetachedFromWindow");

		getContext().unregisterReceiver(this.mIntentReceiver);

		getContext().unregisterReceiver(timeReceiver);
	}

}
